package com.huawei.struts.dao;

import java.sql.SQLException;
import java.util.HashMap;
import java.util.Iterator;
import java.util.List;
import java.util.Map;

import org.apache.commons.logging.Log;
import org.apache.commons.logging.LogFactory;
import org.hibernate.HibernateException;
import org.hibernate.LockMode;
import org.hibernate.Query;
import org.hibernate.Session;
import org.springframework.context.ApplicationContext;
import org.springframework.orm.hibernate3.HibernateCallback;
import org.springframework.orm.hibernate3.support.HibernateDaoSupport;

import com.st.model.CatalogT;
import com.st.model.UserT;

/**
 * A data access object (DAO) providing persistence and search support for
 * CatalogT entities. Transaction control of the save(), update() and delete()
 * operations can directly support Spring container-managed transactions or they
 * can be augmented to handle user-managed Spring transactions. Each of these
 * methods provides additional information for how to configure it for the
 * desired type of transaction control.
 * 
 * @see com.st.model.CatalogT
 * @author MyEclipse Persistence Tools
 */

public class CatalogTDAO extends HibernateDaoSupport {
	private static final Log log = LogFactory.getLog(CatalogTDAO.class);
	// property constants
	public static final String CATALOG_NAME = "catalogName";
	public static final String PARENT_ID = "parentId";
	public static final String LINK_URL = "linkUrl";
	public static final String REMARK = "remark";

	protected void initDao() {
		// do nothing
	}

	public void save(CatalogT transientInstance) {
		log.debug("saving CatalogT instance");
		try {
			getHibernateTemplate().save(transientInstance);
			log.debug("save successful");
		} catch (RuntimeException re) {
			log.error("save failed", re);
			throw re;
		}
	}

	public void delete(CatalogT persistentInstance) {
		log.debug("deleting CatalogT instance");
		try {
			getHibernateTemplate().delete(persistentInstance);
			log.debug("delete successful");
		} catch (RuntimeException re) {
			log.error("delete failed", re);
			throw re;
		}
	}

	public CatalogT findById(java.lang.Integer id) {
		log.debug("getting CatalogT instance with id: " + id);
		try {
			CatalogT instance = (CatalogT) getHibernateTemplate().get(
					"com.st.model.CatalogT", id);
			return instance;
		} catch (RuntimeException re) {
			log.error("get failed", re);
			throw re;
		}
	}

	public List findByExample(CatalogT instance) {
		log.debug("finding CatalogT instance by example");
		try {
			List results = getHibernateTemplate().findByExample(instance);
			log.debug("find by example successful, result size: "
					+ results.size());
			return results;
		} catch (RuntimeException re) {
			log.error("find by example failed", re);
			throw re;
		}
	}

	public List findByProperty(String propertyName, Object value) {
		log.debug("finding CatalogT instance with property: " + propertyName
				+ ", value: " + value);
		try {
			String queryString = "from CatalogT as model where model."
					+ propertyName + "= ?";
			return getHibernateTemplate().find(queryString, value);
		} catch (RuntimeException re) {
			log.error("find by property name failed", re);
			throw re;
		}
	}

	public List findByCatalogName(Object catalogName) {
		return findByProperty(CATALOG_NAME, catalogName);
	}

	public List findByParentId(Object parentId) {
		return findByProperty(PARENT_ID, parentId);
	}

	public List findByLinkUrl(Object linkUrl) {
		return findByProperty(LINK_URL, linkUrl);
	}

	public List findByRemark(Object remark) {
		return findByProperty(REMARK, remark);
	}

	public List findAll() {
		log.debug("finding all CatalogT instances");
		try {
			String queryString = "from CatalogT u order by u.remark asc";
			return getHibernateTemplate().find(queryString);
		} catch (RuntimeException re) {
			log.error("find all failed", re);
			throw re;
		}
	}

	public CatalogT merge(CatalogT detachedInstance) {
		log.debug("merging CatalogT instance");
		try {
			CatalogT result = (CatalogT) getHibernateTemplate().merge(
					detachedInstance);
			log.debug("merge successful");
			return result;
		} catch (RuntimeException re) {
			log.error("merge failed", re);
			throw re;
		}
	}

	public void attachDirty(CatalogT instance) {
		log.debug("attaching dirty CatalogT instance");
		try {
			getHibernateTemplate().saveOrUpdate(instance);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public void attachClean(CatalogT instance) {
		log.debug("attaching clean CatalogT instance");
		try {
			getHibernateTemplate().lock(instance, LockMode.NONE);
			log.debug("attach successful");
		} catch (RuntimeException re) {
			log.error("attach failed", re);
			throw re;
		}
	}

	public static CatalogTDAO getFromApplicationContext(ApplicationContext ctx) {
		return (CatalogTDAO) ctx.getBean("CatalogTDAO");
	}
	
	
	public List showCatalogByParentId(final Integer parentId) {
		List list = getHibernateTemplate().executeFind(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				StringBuffer hql=new StringBuffer();
				hql.append("from CatalogT u where 1=1");
				Query query=queryMap(hql,parentId,session);
				List list = query.list();
				return list;
			}
		});
		return list;
	}
	
	public Integer showCatalogByCatalogId(final Integer parentId) {
		Integer count = (Integer) getHibernateTemplate().execute(new HibernateCallback() {
			public Object doInHibernate(Session session)
					throws HibernateException, SQLException {
				
				StringBuffer hql=new StringBuffer();
				hql.append("select count(*) from CatalogT u where 1=1");
				Query query=queryMap1(hql,parentId,session);
				return Integer.parseInt(query.iterate().next().toString());
			}
		});
		return count; 
	}
	
	
	public  Query queryMap1(final StringBuffer hql,final Integer catalogId,Session session)
	{
		Map map=new HashMap();
		if(catalogId!=null && catalogId.toString().length()>0)
		{
			map.put("catalogId", catalogId);
		    hql.append(" and u.parentId =:catalogId order by u.remark asc");

		}
		Query query = session.createQuery(hql.toString());
		
		Iterator it = map.keySet().iterator();

         while (it.hasNext()){

         Object key = it.next(); 

         query.setParameter(key.toString(), map.get(key)); 

         }
         return query;
	}
	
	public  Query queryMap(final StringBuffer hql,final Integer parentId,Session session)
	{
		Map map=new HashMap();
		if(parentId!=null && parentId.toString().length()>0)
		{
			map.put("parentId", parentId);
		    hql.append(" and u.parentId =:parentId order by u.remark asc");

		}
		Query query = session.createQuery(hql.toString());
		
		Iterator it = map.keySet().iterator();

         while (it.hasNext()){

         Object key = it.next(); 

         query.setParameter(key.toString(), map.get(key)); 

         }
         return query;
	}
}